package com.css.fxfzfxqh.modules.util.hypergraph.service.impl;

import com.css.fxfzfxqh.modules.util.hypergraph.service.AsyncService;
import com.css.fxfzfxqh.modules.util.hypergraph.service.HypergraphService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.Future;

/**
 * @Author:
 * @CreateTime: 2023/11/17 10:39
 * @Description:
 */
@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {

    @Resource
    HypergraphService hypergraphService;

    @Resource
    private TaskExecutor taskExecutor;

    @Async
    @Override
    public Future<Void> asyncRefresh(String datasetName) {
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                long startTime = System.currentTimeMillis();
                try {
                    hypergraphService.getWorkspaceReload(datasetName);
                    log.info("超图服务刷新图层完毕--"+datasetName);
                } catch (Exception e) {
                    String errormessage = datasetName+"--超图刷新失败";
                    log.error(errormessage, e);
                }finally {
                    long endTime = System.currentTimeMillis();
                    long duration = endTime - startTime;
                    log.info("刷新耗时: {} 毫秒", duration);
                }

            }
        });
        return new AsyncResult<>(null);
    }
}
